import ResManager from "../../../core/ResManager";
import { ResType } from "../../../net/Commond";
import { Utils } from "../../../utils/Utils";
import { ElementWord, HeroData, ResPath } from "../../data/CFCommon";
import BattleBuffIcon from "./BattleBuffIcon";

const { ccclass, property } = cc._decorator;

@ccclass
export default class BattleRoleUi extends cc.Component {
    @property(cc.Node)
    node_role: cc.Node = null;

    @property(sp.Skeleton)
    spine_role: sp.Skeleton = null;

    @property(cc.ProgressBar)
    hpProgress: cc.ProgressBar = null;

    @property(cc.ProgressBar)
    powerProgress: cc.ProgressBar = null;

    @property(cc.Node)
    bottomEffects: cc.Node = null;
    @property(cc.Node)
    topEffects: cc.Node = null;
    @property(cc.Node)
    Buffs: cc.Node = null;
    @property(cc.Node)
    lastEffects: cc.Node = null;

    @property(cc.Prefab)
    buffIcon: cc.Prefab = null;

    private isOwner: boolean;
    private maxHp: number = 100;
    private data: HeroData;
    private initColor: cc.Color = cc.Color.WHITE;
    private spineTimeScale: number = 1;

    public init(isOwner: boolean, data: HeroData): void {
        this.maxHp = data.hp
        this.data = data;
        this.hpProgress.progress = 100;
        this.powerProgress.progress = 100;
        this.node_role.scaleX = isOwner ? -1 : 1;
        this.isOwner = isOwner;
        this.loadSpine(data.spinePath, this.spine_role);
    }

    public getData(): HeroData {
        return this.data
    }

    loadSpine(url: string, spine: sp.Skeleton) {
        ResManager.ins().loadSingle(url, ResType.spine, ResPath.hero_spinepath, (asset) => {
            spine.skeletonData = asset;
            spine.setAnimation(0, 'idle', true);
        }, this.node);
    }

    doAction(_animation: string, _loop: boolean = false, callback = null) {
        this.spine_role.setAnimation(0, _animation, _loop);
        if (_animation != 'die') {
            this.spine_role.addAnimation(0, 'idle', true);
        }
    }

    onDead() {
        this.doAction('die', false);
        this.scheduleOnce(function () {
            this.node_role.active = false;
            this.node.active = false;
        }, 0.7);
    }

    updateHp(value: number) {
        this.data.hp = this.data.hp + value;
        let pr = Math.max(0, this.data.hp / this.maxHp);
        this.hpProgress.progress = pr;
        this.addFightWord(value);
    }

    getHp() {
        return this.data.hp;
    }

    updatePower(pr: number) {
        this.data.power = this.data.power + pr;
        pr = Math.max(0, this.data.power);
        this.powerProgress.progress = pr;
    }

    getPower() {
        return this.data.power;
    }

    addFightWord(value: number, fightWord: string = ElementWord.hit) {
        ResManager.ins().loadSingle(fightWord, ResType.prefab, ResPath.number_effects, (asset) => {
            let newNode = new cc.Node()
            newNode.scale = 0.5
            let newpos = cc.v3(this.node.position.x + Utils.getRandomInt(-30, 30), this.node.position.y + Utils.getRandomInt(20, 60), 0)
            newNode.position = newpos
            this.node.addChild(newNode)
            let _node = cc.instantiate(asset)
            //修改数字
            if (value !== null) {
                let hurt = _node.getChildByName('hurt_label')
                if (hurt) {
                    // hurt.color = new cc.Color(255, 0, 0);
                    let hurt_label = hurt.getComponent(cc.Label)
                    hurt_label && (hurt_label.string = value)
                }
            }
            newNode.addChild(_node)
            let _anim = _node.getComponent(cc.Animation)
            _anim.on('finished', function () {
                newNode.destroy()
            });
        }, this.node);
    }

    //添加效果
    addEffect(anim, top = true, scaleX = 1, callback = null) {
        let node = this.node;
        // if (top) {
        //     node = this.topEffects
        // } else {
        //     node = this.bottomEffects
        // }
        if (!anim) {
            return
        }
        ResManager.ins().loadSingle(anim, ResType.prefab, ResPath.hitEffect, (asset) => {
            let newNode = new cc.Node()
            newNode.scaleX = scaleX
            node.addChild(newNode)
            let _node = cc.instantiate(asset)
            newNode.y = top ? 50 : 0
            newNode.addChild(_node)
            let _anim = _node.getComponent(cc.Animation)
            _anim.on('finished', function () {
                newNode.destroy();
            });
        }, this.node);
    }

    updateElement(data) {
        let config_skillElement = ResManager.ins().getRes('config_skillElement')
        let skillElement = config_skillElement[data.elementId]
        let nodeName
        if (skillElement.effectType == 2) {//BUFF
            nodeName = 'buff' + data.elementId
        } else if (skillElement.effectType == 3) {//状态
            nodeName = 'state' + skillElement.attrType
        } else if (skillElement.effectType == 4) {//护盾
            nodeName = 'shield' + skillElement.attrType
        }
        if (!data.add) {//回合为0则表示删除
            if (skillElement.elementIcon) {
                let buffIcon = this.Buffs.getChildByName(nodeName)
                if (buffIcon) {
                    let buffCtrl = buffIcon.getComponent('battleBuffIcon')
                    buffCtrl.decRound()
                }
            }
            if (skillElement.lastEffect) {
                if (skillElement.lastEffect == 'buffeffects1007') {
                    this.spine_role.node.color = this.initColor
                }
                let lastEffect = this.lastEffects.getChildByName(nodeName)
                if (lastEffect) { lastEffect.destroy() }
            }
            //冰冻和眩晕暂停人物SPINE动画
            if (skillElement.effectType == 3 && skillElement.result == -1 && (skillElement.attrType == 1 || skillElement.attrType == 2)) {
                this.spine_role.setAnimation(0, 'idle', true)
                this.spine_role.timeScale = this.spineTimeScale
            }
        } else {
            //冰冻和眩晕暂停人物SPINE动画
            if (skillElement.effectType == 3 && skillElement.result == -1 && (skillElement.attrType == 1 || skillElement.attrType == 2)) {
                this.spine_role.setAnimation(0, 'idle', true)
                this.spine_role.timeScale = 0
            }

            if (skillElement.elementIcon) {
                let buffIcon = this.Buffs.getChildByName(nodeName)
                if (buffIcon) {
                    let buffCtrl = buffIcon.getComponent(BattleBuffIcon)
                    buffCtrl.addRound()
                } else {
                    buffIcon = cc.instantiate(this.buffIcon)
                    this.Buffs.addChild(buffIcon)
                    buffIcon.name = nodeName
                    let buffCtrl = buffIcon.getComponent(BattleBuffIcon)
                    buffCtrl.init(skillElement)
                }
            }
            if (skillElement.lastEffect) {
                if (skillElement.lastEffect == 'buffeffects1007') {
                    this.spine_role.node.color = cc.Color.GREEN
                }
                let lastEffect = this.lastEffects.getChildByName(nodeName)
                if (!lastEffect) {
                    let newNode = new cc.Node()
                    this.lastEffects.addChild(newNode)
                    newNode.name = nodeName
                    newNode.y = 50
                    ResManager.ins().loadSingle(skillElement.lastEffect, ResType.prefab, ResPath.buff, function (asset) {
                        if (newNode.isValid) {
                            let _node = cc.instantiate(asset)
                            newNode.addChild(_node)
                            let _anim = _node.getComponent(cc.Animation)
                            _anim.on('finished', function () {
                                this.spine_role.node.color = this.initColor
                                newNode.destroy()
                            });
                        }
                    }, this.node);
                }
            }
        }
    }

}
